
import express from 'express'
import { LogicServer } from '../LogicServer';
import { EnumCacheGroup } from '../../../common/enum/EnumCacheGroup';
import { EnumErrorCode } from '../../../common/enum/EnumErrorCode';
import { UserData } from '../../../common/model/UserData';
import { CS_Login, SC_Login } from '../../../common/protocols/Proto_Login';
import { CS_Register } from '../../../common/protocols/Proto_Register';
import { CacheComponent } from '../../../framework/service/Components/CacheComponent';
import { IMsg } from '../../../framework/service/Components/WorkOrderInfo';
import { Tools } from '../../../framework/utils/Tools';


export const UserRouter = express.Router();



UserRouter.post("/user/login", async (req, res) => {
    const cs: CS_Login = req.body;
    const sc: SC_Login = new SC_Login();


    const cacheComp: CacheComponent = <CacheComponent>LogicServer.entity.getComponent(CacheComponent);

    const userdata: UserData = await cacheComp.get(EnumCacheGroup.USER_DATA, cs.username);





    if (!userdata) {
        sc.errorcode = EnumErrorCode.Account_notFound;
        res.send(sc);
        return;
    }
    const oldSession: string = userdata.basic.session;
    userdata.basic.session = Tools.getUID();


    await cacheComp.set(EnumCacheGroup.USER_SESSION, userdata.basic.session, userdata, 60);

    await cacheComp.set(EnumCacheGroup.USER_DATA, userdata.basic.username, userdata);
    if (oldSession.length > 0) {
        await cacheComp.del(EnumCacheGroup.USER_SESSION, oldSession);
    }
    
    if (userdata.basic.password != cs.password) {
        sc.errorcode = EnumErrorCode.Password_Error;
        res.send(sc);
        return;
    }

    if (!cacheComp) {
        sc.errorcode = EnumErrorCode.RPCComponentNotFound;
        res.send(sc);
        return;
    }

    sc.session = userdata.basic.session;
    sc.userid = userdata.uid;
    res.send(sc);




    // try{
    //     const user: User = await DBManager.inst.findUser({ email: data.email });

    //     if (!user) {
    //         sc.tip(EnumErrorCode.Account_Exist);
    //         res.send(sc);
    //         return;
    //     }

    //     // 检查密码是否正确
    //     if (user.password != data.password) {
    //         sc.tip(EnumErrorCode.Password_Error);
    //         res.send(sc);
    //         return;
    //     }
    //     user.last_login_ip = req.ip;
    //     user.last_login_time = Tools.getServerDate();
    //     sc.userid = user.id;
    //     sc.name = user.nickname;
    //     sc.session = await DBManager.inst.updateUserSession(user);
    //     res.send(sc);

    // }
    // catch(err){
    //     sc.tip(EnumErrorCode.Login_fail);
    //     console.log(err);
    //     res.send(sc);
    // }

});


UserRouter.post("/logic", async (req, res) => {
    const cs: IMsg = req.body;


    // if (!cs.extra.session) {
    //     const sc: SC_SystemError = new SC_SystemError();
    //     sc.code = EnumErrorCode.Session_isInvalid;
    //     res.send(sc);
    //     return;
    // }
    
    // const cacheComp: CacheComponent = <CacheComponent>LogicServer.entity.getComponent(CacheComponent);

    // const userData: UserData = await cacheComp.get(EnumCacheGroup.USER_SESSION, cs.extra.session);

    // if (!userData) {
    //     const sc:SC_SystemError = new SC_SystemError();
    //     sc.code = EnumErrorCode.Session_isInvalid;
    //     res.send(sc);
    //     return;
    // }
    // await cacheComp.set(EnumCacheGroup.USER_SESSION, userData.basic.session, userData, 60);


    // const sc:IMsg = await LogicCenter.inst.commingRequest(cs,userData);
    // if(sc){
    //     res.send(sc);

    // }
    // else{
    //     const sc:SC_SystemError = new SC_SystemError();
    //     sc.code = EnumErrorCode.ResponseTimOut;
    //     res.send(sc);
    // }
    



    // try{
    //     const user: User = await DBManager.inst.findUser({ email: data.email });

    //     if (!user) {
    //         sc.tip(EnumErrorCode.Account_Exist);
    //         res.send(sc);
    //         return;
    //     }

    //     // 检查密码是否正确
    //     if (user.password != data.password) {
    //         sc.tip(EnumErrorCode.Password_Error);
    //         res.send(sc);
    //         return;
    //     }
    //     user.last_login_ip = req.ip;
    //     user.last_login_time = Tools.getServerDate();
    //     sc.userid = user.id;
    //     sc.name = user.nickname;
    //     sc.session = await DBManager.inst.updateUserSession(user);
    //     res.send(sc);

    // }
    // catch(err){
    //     sc.tip(EnumErrorCode.Login_fail);
    //     console.log(err);
    //     res.send(sc);
    // }

});
UserRouter.post("/user/register", async (req, res) => {
    const cs: CS_Register = req.body;

    const sc: SC_Login = new SC_Login();

    const cacheComp:CacheComponent  = <CacheComponent>LogicServer.entity.getComponent(CacheComponent);

    const userdata:UserData = await cacheComp.get(EnumCacheGroup.USER_DATA,cs.username);

    if(userdata){
        sc.errorcode = EnumErrorCode.Account_Exist;
        res.send(sc);
        return;
    }

    const user:UserData = new UserData();
    user.basic.username = cs.username;
    user.basic.password = cs.password;
    sc.userid = user.uid;

    await cacheComp.set(EnumCacheGroup.USER_DATA,cs.username,user);
    sc.userid = user.uid;
    res.send(sc);

});


export default UserRouter;